package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysPerson;
import com.ruoyi.system.service.ISysPersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人员基本信息Controller
 * 
 * @author ruoyi
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/system/person")
public class SysPersonController extends BaseController
{
    @Autowired
    private ISysPersonService sysPersonService;

    @Autowired
    private ISysPostService postService;

    /**
     * 查询人员基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPerson sysPerson)
    {
        startPage();
        List<SysPerson> list = sysPersonService.selectSysPersonList(sysPerson);
        return getDataTable(list);
    }

    /**
     * 导出人员基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:person:export')")
    @Log(title = "人员基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPerson sysPerson)
    {
        List<SysPerson> list = sysPersonService.selectSysPersonList(sysPerson);
        ExcelUtil<SysPerson> util = new ExcelUtil<SysPerson>(SysPerson.class);
        util.exportExcel(response, list, "人员基本信息数据");
    }

    /**
     * 获取人员基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:query')")
    @GetMapping(value ={"/", "/{personId}"})
    public AjaxResult getInfo(@PathVariable(value = "personId", required = false) Long personId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(personId))
        {
            SysPerson sysPerson = sysPersonService.selectSysPersonByPersonId(personId);
            ajax.put(AjaxResult.DATA_TAG, sysPerson);
            ajax.put("rankIds", postService.selectPostById(personId));
        }
        return ajax;
    }

    /**
     * 新增人员基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:add')")
    @Log(title = "人员基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPerson sysPerson)
    {
        return toAjax(sysPersonService.insertSysPerson(sysPerson));
    }

    /**
     * 修改人员基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:edit')")
    @Log(title = "人员基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
        public AjaxResult edit(@RequestBody SysPerson sysPerson)
    {
        return toAjax(sysPersonService.updateSysPerson(sysPerson));
    }

    /**
     * 删除人员基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:remove')")
    @Log(title = "人员基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{personIds}")
    public AjaxResult remove(@PathVariable Long[] personIds)
    {
        return toAjax(sysPersonService.deleteSysPersonByPersonIds(personIds));
    }
}
